<?php
use TbfDatabase\MysqlAST\SelectCommand;

class TbfDatabaseTest extends TbfTestCase{
    function test1(){
        TbfDatabase::execute('DROP TABLE IF EXISTS `tbf_test`');
        TbfDatabase::execute('CREATE TABLE `tbf_test`(
    id int auto_increment,
    real_name varchar(255),
    primary key (id)
)engine=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin');
        $data = TbfDatabase::query('SELECT * FROM `tbf_test`');
        $this->assertSame(array(),$data);

        TbfDatabase::insert('tbf_test',array('real_name'=>'abc'));
        $row = TbfDatabase::getOneById('tbf_test',1);
        $this->assertSame('abc',$row['real_name']);

        $data = TbfDatabase::getAllInTable('tbf_test');
        $this->assertSame(1,count($data));

        TbfDatabase::updateById('tbf_test',array('id'=>1,'real_name'=>'abcd'));

        $row = TbfDatabase::getOneById('tbf_test',1);
        $this->assertSame('abcd',$row['real_name']);

        TbfDatabase::replaceById('tbf_test',array('id'=>1,'real_name'=>'abcde'));

        $row = TbfDatabase::getOneById('tbf_test',1);
        $this->assertSame('abcde',$row['real_name']);

        TbfDatabase::replaceById('tbf_test',array('id'=>2,'real_name'=>'abc'));

        $row = TbfDatabase::getOneById('tbf_test',2);
        $this->assertSame('abc',$row['real_name']);

        $obj = TbfDatabase::ormFromArray(new TbfTestOrmObject(),$row);
        $this->assertSame('2',(string)$obj->id);
        $this->assertSame('abc',$obj->real_name);

        $obj->real_name='abcd';
        TbfDatabase::ormPersist($obj);

        $row = TbfDatabase::getOneById('tbf_test',2);
        $this->assertSame('abcd',$row['real_name']);
    }

    function testQuery(){
        TbfDatabase::query('DROP TABLE IF EXISTS `tbf_test`');
        TbfDatabase::query('CREATE TABLE `tbf_test`(
    id int auto_increment,
    real_name varchar(255),
    primary key (id)
)engine=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin');
        $data = TbfDatabase::query('SELECT * FROM `tbf_test`');
        $this->assertSame(array(),$data);

        $data = TbfDatabase::query('SELECT * FROM `tbf_test`');
        $this->assertSame(array(),$data);
    }

    function testRunSelectCommand(){
        TbfDatabase::query('DROP TABLE IF EXISTS `tbf_test`');
        TbfDatabase::query('CREATE TABLE `tbf_test`(
    id int auto_increment,
    real_name varchar(255),
    primary key (id)
)engine=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin');
        TbfDatabase::setDatabaseDataByYaml(<<<'YML'
tbf_test:
    - id: 1
      real_name: 1
    - id: 2
      real_name: 2
YML
);
        $select = SelectCommand::create()->from('tbf_test')->where('real_name=1');
        $data = TbfDatabase::runSelectCommand($select);
        self::assertSame(1,count($data));
    }
}

class TbfTestOrmObject implements \TbfDatabase\Orm\OrmObject{
    public $id;
    public $real_name;
    /**
     * 获取主键名
     * @return string
     */
    function getIdFieldName(){
        return 'id';
    }

    /**
     * 表名
     * @return string
     */
    function getTableName(){
        return 'tbf_test';
    }

}